/*
 * 文件名：AddressController.java
 * 版权：
 * 描述：TODO
 * 修改人：zuo liubo
 * 修改时间：2013-2-2 下午4:15:43
 */
package com.scpii.api.controller.ec;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.base.BaseController;
import com.scpii.api.common.exception.UserIdDisaccordException;
import com.scpii.api.domain.ec.Address;
import com.scpii.api.service.ec.AddressService;
import com.scpii.api.util.PrincipalUtil;

/**
 * @description 地址Controller类
 * @author zuo liubo
 * @date 2013-2-2
 */
@Controller
@RequestMapping("/ec/address")
public class AddressController extends BaseController {

	int affectRow = 0;

	@Resource
	private AddressService addressService;

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public Address getAddressByUserId(@PathVariable("userId") Long userId)
			throws Exception {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		if (!_userId.equals(userId)) {
			throw new UserIdDisaccordException();
		}
		List<Address> addressList = addressService.getAddressByUserId(userId);
		if (addressList.size() > 0) {
			Address address = addressList.get(0);
			address.setLati(null);
			address.setLongi(null);
			address.setTargetId(null);
			return address;
		}
		return null;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertAddress(Address address, Long userId) {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		address.setTargetId(userId);
		address.setTargetId(_userId);
		address.setCreateDt(new Date());
		address.setCreateBy(_userId);
		Long addressId = addressService.insertAddress(address);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("addressId", addressId);
		return map;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/{addressId}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> updateAddress(@PathVariable("addressId") Long addressId,
			Address address) {
		Long _userId = PrincipalUtil.getPrincipal().getId();
		address.setAddressId(addressId);
		address.setUpdateDt(new Date());
		address.setUpdateBy(_userId);
		addressId = addressService.updateAddress(address);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("addressId", addressId);
		return map;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Boolean deleteAddress(@PathVariable("id") Long id) {
		return addressService.deleteAddressById(id);
	}

	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/target/{targetId}", method = RequestMethod.GET)
	@ResponseBody
	public List<Address> getAddressByTargetId(
			@PathVariable("targetId") Long targetId) {
		return addressService.selectByTargetId(targetId);
	}
}
